<?php
// +------------------------------------------------------------------------+
// | PHP Version 5.4                                                        |
// +------------------------------------------------------------------------+
// | Copyright (c) All rights reserved.                                     |
// +------------------------------------------------------------------------+
// | 世纪车来车往(北京）网络科技有限公司 版权所有！                                 |
// | TEL:010-63732365  HOTLINE:400-6688-365  EMAIL:clcw@clcw.com.cn         |
// | $Revision: 1.0 $                                                       |
// +------------------------------------------------------------------------+
// | Author: xiangminghu@clcw.com.cn                                        |
// +------------------------------------------------------------------------+
//

namespace AuctionBack\Controller;

use AuctionCore\AntTrade\AntTradeRefundLog;
use AuctionCore\Question\QuestionOrder;

class NotifyController extends BaseController
{
    private $questionOrder;
    private $antTradeRefundLog;

    public function __construct()
    {
        parent::__construct();
        $this->questionOrder = new QuestionOrder();
        $this->antTradeRefundLog = new AntTradeRefundLog();
    }

    /**
     * 提问付费订单退款回调
     */
    public function refund()
    {
        $orderSN = I('post.order_sn', '', 'trim');
        $refundAmount = I('post.refund_amount');
        $payCode = I('post.pay_code', '', 'trim');
        $refundStatus = I('post.refund_status', '', 'trim');
        $refundTime = I('post.refund_time');
        $logData = [
            'order_sn' => $orderSN,
            'refund_amount' => $refundAmount,
            'pay_code' => $payCode,
            'refund_status' => $refundStatus,
            'refund_time' => $refundTime
        ];
        if (empty($orderSN) || empty($refundAmount) || empty($payCode)
            || empty($refundStatus) || empty($refundTime)
        ) {
            $logData['code'] = '1700000';
            $this->antTradeRefundLog->addAntTradeRefundLog($logData);
            $this->helpJsonResult('400','参数不能为空');
        }
        $order = $this->questionOrder->getQuestionOrderByOrderSn($orderSN);
        if (empty($order)) {
            $logData['code'] = '1700001';
            $this->antTradeRefundLog->addAntTradeRefundLog($logData);
            $this->helpJsonResult('400','无效的订单');
        }
        if ($refundStatus == 'success') {
            $order['status'] = 3;
            $order['return_time'] = $refundTime;
        }
        $result = $this->questionOrder->updateQuestionOrder($order);
        if (!$result) {
            $logData['code'] = '1700003';
            $this->antTradeRefundLog->addAntTradeRefundLog($logData);
            $this->helpJsonResult('400','修改订单失败');
        }
        $logData['code'] = '200';
        $this->antTradeRefundLog->addAntTradeRefundLog($logData);
        $this->helpJsonResult('200','修改成功');
    }
    
    
    /**
     * 加油卡充值退款回调
     */
    public function OilPayRefund()
    {
        $orderSN = I('post.order_sn', '', 'trim');
        $refundAmount = I('post.refund_amount');
        $payCode = I('post.pay_code', '', 'trim');
        $refundStatus = I('post.refund_status', '', 'trim');
        $refundTime = I('post.refund_time');
        $logData = [
            'order_sn' => $orderSN,
            'refund_amount' => $refundAmount,
            'pay_code' => $payCode,
            'refund_status' => $refundStatus,
            'refund_time' => $refundTime
        ];
        if (empty($orderSN) || empty($refundAmount) || empty($payCode)
            || empty($refundStatus) || empty($refundTime)
        ) {
            $logData['code'] = '1700000';
            $this->antTradeRefundLog->addAntTradeRefundLog($logData);
            $this->helpJsonResult('400','参数不能为空');
        }
        $order = \AuctionCore\OilPayOrder\OilPayOrder::getOrderBySn($orderSN);
        if (empty($order)) {
            $logData['code'] = '1700001';
            $this->antTradeRefundLog->addAntTradeRefundLog($logData);
            $this->helpJsonResult('400','无效的订单');
        }
        if ($refundStatus == 'success') {
            $data['status'] = 5;
            $data['update_time'] = NOW_TIME;
            $data['return_time'] = $refundTime;
        }
        $result = \AuctionCore\OilPayOrder\OilPayOrder::saveOrderBySn($orderSN, $data);
        if (!$result) {
            $logData['code'] = '1700003';
            $this->antTradeRefundLog->addAntTradeRefundLog($logData);
            $this->helpJsonResult('400','修改订单失败');
        }
        $logData['code'] = '200';
        $this->antTradeRefundLog->addAntTradeRefundLog($logData);
        $this->helpJsonResult('200','修改成功');
    }
    
}